/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ep.persistence.controllers;

import ep.persistence.controllers.exceptions.IllegalOrphanException;
import ep.persistence.controllers.exceptions.NonexistentEntityException;
import ep.persistence.entities.T007Illness;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import ep.persistence.entities.T005T007;
import java.util.ArrayList;
import java.util.Collection;
import ep.persistence.entities.T010Substance;

/**
 *
 * @author Spyros
 */
public class T007IllnessJpaController {

    public T007IllnessJpaController() {
        emf = Persistence.createEntityManagerFactory("EprescPersistencePU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(T007Illness t007Illness) {
        if (t007Illness.getT005T007Collection() == null) {
            t007Illness.setT005T007Collection(new ArrayList<T005T007>());
        }
        if (t007Illness.getT010Substances() == null) {
            t007Illness.setT010Substances(new ArrayList<T010Substance>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<T005T007> attachedT005T007Collection = new ArrayList<T005T007>();
            for (T005T007 t005T007CollectionT005T007ToAttach : t007Illness.getT005T007Collection()) {
                t005T007CollectionT005T007ToAttach = em.getReference(t005T007CollectionT005T007ToAttach.getClass(), t005T007CollectionT005T007ToAttach.getId());
                attachedT005T007Collection.add(t005T007CollectionT005T007ToAttach);
            }
            t007Illness.setT005T007Collection(attachedT005T007Collection);
            Collection<T010Substance> attachedT010Substances = new ArrayList<T010Substance>();
            for (T010Substance t010SubstancesT010SubstanceToAttach : t007Illness.getT010Substances()) {
                t010SubstancesT010SubstanceToAttach = em.getReference(t010SubstancesT010SubstanceToAttach.getClass(), t010SubstancesT010SubstanceToAttach.getId());
                attachedT010Substances.add(t010SubstancesT010SubstanceToAttach);
            }
            t007Illness.setT010Substances(attachedT010Substances);
            em.persist(t007Illness);
            for (T005T007 t005T007CollectionT005T007 : t007Illness.getT005T007Collection()) {
                T007Illness oldT007IllnessOfT005T007CollectionT005T007 = t005T007CollectionT005T007.getT007Illness();
                t005T007CollectionT005T007.setT007Illness(t007Illness);
                t005T007CollectionT005T007 = em.merge(t005T007CollectionT005T007);
                if (oldT007IllnessOfT005T007CollectionT005T007 != null) {
                    oldT007IllnessOfT005T007CollectionT005T007.getT005T007Collection().remove(t005T007CollectionT005T007);
                    oldT007IllnessOfT005T007CollectionT005T007 = em.merge(oldT007IllnessOfT005T007CollectionT005T007);
                }
            }
            for (T010Substance t010SubstancesT010Substance : t007Illness.getT010Substances()) {
                t010SubstancesT010Substance.getT007Illnesses().add(t007Illness);
                t010SubstancesT010Substance = em.merge(t010SubstancesT010Substance);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(T007Illness t007Illness) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            T007Illness persistentT007Illness = em.find(T007Illness.class, t007Illness.getId());
            Collection<T005T007> t005T007CollectionOld = persistentT007Illness.getT005T007Collection();
            Collection<T005T007> t005T007CollectionNew = t007Illness.getT005T007Collection();
            Collection<T010Substance> t010SubstancesOld = persistentT007Illness.getT010Substances();
            Collection<T010Substance> t010SubstancesNew = t007Illness.getT010Substances();
            List<String> illegalOrphanMessages = null;
            for (T005T007 t005T007CollectionOldT005T007 : t005T007CollectionOld) {
                if (!t005T007CollectionNew.contains(t005T007CollectionOldT005T007)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain T005T007 " + t005T007CollectionOldT005T007 + " since its t007Illness field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<T005T007> attachedT005T007CollectionNew = new ArrayList<T005T007>();
            for (T005T007 t005T007CollectionNewT005T007ToAttach : t005T007CollectionNew) {
                t005T007CollectionNewT005T007ToAttach = em.getReference(t005T007CollectionNewT005T007ToAttach.getClass(), t005T007CollectionNewT005T007ToAttach.getId());
                attachedT005T007CollectionNew.add(t005T007CollectionNewT005T007ToAttach);
            }
            t005T007CollectionNew = attachedT005T007CollectionNew;
            t007Illness.setT005T007Collection(t005T007CollectionNew);
            Collection<T010Substance> attachedT010SubstancesNew = new ArrayList<T010Substance>();
            for (T010Substance t010SubstancesNewT010SubstanceToAttach : t010SubstancesNew) {
                t010SubstancesNewT010SubstanceToAttach = em.getReference(t010SubstancesNewT010SubstanceToAttach.getClass(), t010SubstancesNewT010SubstanceToAttach.getId());
                attachedT010SubstancesNew.add(t010SubstancesNewT010SubstanceToAttach);
            }
            t010SubstancesNew = attachedT010SubstancesNew;
            t007Illness.setT010Substances(t010SubstancesNew);
            t007Illness = em.merge(t007Illness);
            for (T005T007 t005T007CollectionNewT005T007 : t005T007CollectionNew) {
                if (!t005T007CollectionOld.contains(t005T007CollectionNewT005T007)) {
                    T007Illness oldT007IllnessOfT005T007CollectionNewT005T007 = t005T007CollectionNewT005T007.getT007Illness();
                    t005T007CollectionNewT005T007.setT007Illness(t007Illness);
                    t005T007CollectionNewT005T007 = em.merge(t005T007CollectionNewT005T007);
                    if (oldT007IllnessOfT005T007CollectionNewT005T007 != null && !oldT007IllnessOfT005T007CollectionNewT005T007.equals(t007Illness)) {
                        oldT007IllnessOfT005T007CollectionNewT005T007.getT005T007Collection().remove(t005T007CollectionNewT005T007);
                        oldT007IllnessOfT005T007CollectionNewT005T007 = em.merge(oldT007IllnessOfT005T007CollectionNewT005T007);
                    }
                }
            }
            for (T010Substance t010SubstancesOldT010Substance : t010SubstancesOld) {
                if (!t010SubstancesNew.contains(t010SubstancesOldT010Substance)) {
                    t010SubstancesOldT010Substance.getT007Illnesses().remove(t007Illness);
                    t010SubstancesOldT010Substance = em.merge(t010SubstancesOldT010Substance);
                }
            }
            for (T010Substance t010SubstancesNewT010Substance : t010SubstancesNew) {
                if (!t010SubstancesOld.contains(t010SubstancesNewT010Substance)) {
                    t010SubstancesNewT010Substance.getT007Illnesses().add(t007Illness);
                    t010SubstancesNewT010Substance = em.merge(t010SubstancesNewT010Substance);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = t007Illness.getId();
                if (findT007Illness(id) == null) {
                    throw new NonexistentEntityException("The t007Illness with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            T007Illness t007Illness;
            try {
                t007Illness = em.getReference(T007Illness.class, id);
                t007Illness.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The t007Illness with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<T005T007> t005T007CollectionOrphanCheck = t007Illness.getT005T007Collection();
            for (T005T007 t005T007CollectionOrphanCheckT005T007 : t005T007CollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This T007Illness (" + t007Illness + ") cannot be destroyed since the T005T007 " + t005T007CollectionOrphanCheckT005T007 + " in its t005T007Collection field has a non-nullable t007Illness field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<T010Substance> t010Substances = t007Illness.getT010Substances();
            for (T010Substance t010SubstancesT010Substance : t010Substances) {
                t010SubstancesT010Substance.getT007Illnesses().remove(t007Illness);
                t010SubstancesT010Substance = em.merge(t010SubstancesT010Substance);
            }
            em.remove(t007Illness);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<T007Illness> findT007IllnessEntities() {
        return findT007IllnessEntities(true, -1, -1);
    }

    public List<T007Illness> findT007IllnessEntities(int maxResults, int firstResult) {
        return findT007IllnessEntities(false, maxResults, firstResult);
    }

    private List<T007Illness> findT007IllnessEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(T007Illness.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public T007Illness findT007Illness(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(T007Illness.class, id);
        } finally {
            em.close();
        }
    }

    public int getT007IllnessCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<T007Illness> rt = cq.from(T007Illness.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
